package com.probiz.estore.product.service.impl;

import java.util.List;

import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.ProductDao;
import com.probiz.estore.product.model.ProductSearchCriteria;
import com.probiz.estore.product.service.ProductManager;


/**
 * Manager implementation for Product, responsible for business processing, and communicate between web and persistence layer.
 */
public class ProductManagerImpl extends GenericManagerImpl<Product> implements ProductManager {

	private ProductDao productDao = null;
	
	private ProductCategoryManager productCategoryManager = null;
	
	/**
	 * @param productDao
	 *            the productDao to set
	 */
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	
	

	public void setProductCategoryManager(
			ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}



	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = productDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(Product entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(Product entity) {

	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#countProductByCategoryId(java.lang.Integer)
	 */
	public Integer countProductByCategoryId(Integer categoryId){
		return productDao.countProductByCategoryId(categoryId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#countProductIncludeAllByCategory(com.probiz.estore.catalog.model.ProductCategory)
	 */
	public Integer countProductIncludeAllByCategory(ProductCategory productCategory){
		return productDao.countProductIncludeAllByCategory(productCategory);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#countActiveProductIncludeAllByCategory(com.probiz.estore.catalog.model.ProductCategory)
	 */
	public Integer countActiveProductIncludeAllByCategory(ProductCategory productCategory){
		return productDao.countActiveProductIncludeAllByCategory(productCategory);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#getProductByProductCode(java.lang.String)
	 */
	public Product getProductByProductCode(String productCode) {
		return productDao.getUniqueByProperty("productCode", productCode);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#getSizeByProductCodeByNotProductId(java.lang.String, java.lang.Integer)
	 */
    public Long getSizeByProductCodeByNotProductId(String productCode,Integer productId){
    	return productDao.getSizeByProductCodeByNotProductId(productCode, productId);
    }
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#searchByCriteria(com.probiz.estore.product.model.ProductSearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<Product> searchByCriteria(ProductSearchCriteria productSearchCriteria,SearchCriteria searchCriteria){
		return productDao.searchByCriteria(productSearchCriteria, searchCriteria);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#searchByCriteriaForController(com.probiz.estore.product.model.ProductSearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<Product> searchByCriteriaForController(ProductSearchCriteria productSearchCriteria,SearchCriteria searchCriteria) {
	    if (searchCriteria.getPageNo() < 1)  //如果是翻页过前，就拿第一页就可以。
        {
            searchCriteria.changePaging(1, searchCriteria.getPageSize());
        }
	    List<Product> result = searchByCriteria(productSearchCriteria, searchCriteria);
	    if (result == null|| result.size() == 0 )
	    {
	        if (searchCriteria.getPageNo() > searchCriteria.getTotalPageCount()) //如果是翻页前，就拿最后一页就可以。
	        {
	            searchCriteria.changePaging(searchCriteria.getTotalPageCount(), searchCriteria.getPageSize());
	            result = searchByCriteria(productSearchCriteria, searchCriteria);
	        }
	    }
		return result;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#getNumOfProductsByStoreId(java.lang.Integer)
	 */
	public Long[] getNumOfProductsByStoreId(Integer storeId){
		return productDao.getNumOfProductsByStoreId(storeId);
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#getProductIdByUri(java.lang.String)
	 */
	public Integer getProductIdByUri(String uri) {
		String productId = uri.substring(uri.lastIndexOf("/") + 1, uri
				.lastIndexOf("."));
		productId = productId.substring("item".length());
		return Integer.valueOf(productId);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#getUriByProduct(com.probiz.estore.product.model.Product)
	 */
	public String getUriByProduct(Product product) {
		StringBuffer url = new StringBuffer();
		url.append("/prod");
		for (ProductCategory productCategory : productCategoryManager.getNavigationCategories(product.getCategoryId())) {
			url.append("-");
			url.append(productCategory.getCategoryCode());
		}
		url.append("/item");
		url.append(product.getProductId().toString());
		url.append(".html");
		return url.toString();
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#getReviewUriByProduct(com.probiz.estore.common.model.product.Product)
	 */
	public String getReviewUriByProduct(Product product){
		StringBuffer url = new StringBuffer();
		url.append("/review");
		for (ProductCategory productCategory : productCategoryManager.getNavigationCategories(product.getCategoryId())) {
			url.append("-");
			url.append(productCategory.getCategoryCode());
		}
		url.append("/item");
		url.append(product.getProductId().toString());
		url.append(".html");
		return url.toString();
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#getConsultUriByProduct(com.probiz.estore.common.model.product.Product)
	 */
	public String getConsultUriByProduct(Product product){
		StringBuffer url = new StringBuffer();
		url.append("/consult");
		for (ProductCategory productCategory : productCategoryManager.getNavigationCategories(product.getCategoryId())) {
			url.append("-");
			url.append(productCategory.getCategoryCode());
		}
		url.append("/item");
		url.append(product.getProductId().toString());
		url.append(".html");
		return url.toString();
	}
	
	public String getStockNotifyUriByProduct(Product product){
		StringBuffer url = new StringBuffer();
		url.append("/stock-notify");
		for (ProductCategory productCategory : productCategoryManager.getNavigationCategories(product.getCategoryId())) {
			url.append("-");
			url.append(productCategory.getCategoryCode());
		}
		url.append("/item");
		url.append(product.getProductId().toString());
		url.append(".html");
		return url.toString();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductManager#getProductsByCategoryAndDisplayFlag(java.lang.Integer, java.lang.Short, int)
	 */
	public List<Product> getProductsByCategoryAndDisplayFlag(final Integer categoryId,final Short displayFlag,final int size){
		ProductCategory productCategory = productCategoryManager.getById(categoryId);
		return productDao.getProductsByCategoryAndDisplayFlag(productCategory, displayFlag, size);
	}
	 /*
	  * (non-Javadoc)
	  * @see com.probiz.estore.product.service.ProductManager#getRandomProductList(int)
	  */
    public List<Product> getRandomProductList(int count){
    	return productDao.getRandomProductList(count);
    }
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.product.service.ProductManager#countProductTotal()
     */
    public Long countProductTotal(){
    	return productDao.countProductTotal();
    }
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.product.service.ProductManager#countShelvedProductTotal()
     */
    public Long countShelvedProductTotal(){
    	return productDao.countShelvedProductTotal();
    }
    /**
	 * 根据fieldName改变产品统计对应field的值。count为改变数量，可以为正，也可以为负。
	 */
	public void updateStatField(final Integer productStatId,final String fieldName,final Integer count){
		productDao.updateStatField(productStatId, fieldName, count);
	}
	
	public List<Product> getListByDelete(){
		return productDao.getListByDelete();
	}
}
